package com.hq.myapplicationdemo.views.adapter;


import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hq.myapplicationdemo.R;
import com.hq.myapplicationdemo.model.PkShopData;
import com.hq.myapplicationdemo.views.view.AnimateScrollView;


import java.util.List;


public class TabAdapter extends RecyclerView.Adapter<TabAdapter.AdapterViewHolder> {

    private  Context mContext;
    private List<PkShopData> datas;
    private int pkCount;
    private RecyclerView recyclerView;
    private boolean isTop;//第一个是否是置顶
    private boolean isDiff;//是否隐藏相同 true隐藏  false 全部显示

    public void setPkCountReduce() {
        this.pkCount =   this.pkCount-1;
    }

    public void setTop(boolean top) {
        isTop = top;
    }

    public void setDiff(boolean diff) {
        isDiff = diff;
    }

    public TabAdapter(Context context, List<PkShopData> datas, int pkCount, RecyclerView recyclerView) {
        this.mContext = context;
        this.datas = datas;
        this.pkCount = pkCount;
        this.recyclerView = recyclerView;
    }

    @Override
    public AdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new AdapterViewHolder(View.inflate(parent.getContext(), R.layout.pkshop_item, null));
    }

    @Override
    public void onBindViewHolder(AdapterViewHolder holder, int position) {
       TextView[]  tab_tv = new TextView[pkCount];
        View[]  view_lefts = new View[pkCount];
        addRow(holder.ll_content, holder.tab_root, tab_tv, view_lefts, pkCount);
        PkShopData data = datas.get(position);
        final boolean b = data.getName().startsWith("|");
        if (b) {
            String title = data.getName().replace("|", "");
            holder.ll_title.setVisibility(View.VISIBLE);
            holder.ll_body.setVisibility(View.GONE);
            holder.textView.setText(title);
        } else {
            holder.ll_title.setVisibility(View.GONE);
            holder.ll_body.setVisibility(View.VISIBLE);
            holder.textBody.setText(data.getName());
        }

        for (int i = 0; i < pkCount; i++) {
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if (isDiff && data.isDiff()&!b) {
                //隐藏相同
                //隐藏item要把高度宽度设为0；
                holder.ll_content.setVisibility(View.GONE);
                layoutParams.height = 0;
                layoutParams.width = 0;
            } else {
                //显示item要重新设置高度宽度；
                holder.ll_content.setVisibility(View.VISIBLE);
                layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            }
            holder.ll_content.setLayoutParams(layoutParams);
            TextView v = tab_tv[i];
            View viewLeft = view_lefts[i];
            String str = data.getArray().get(i);
            v.setText(str);
            if (!data.isDiff()&&str.length()>0&!b) {
                v.setBackgroundColor(Color.parseColor("#FDF1EE"));
            } else {
                v.setBackgroundColor(Color.parseColor("#FFFFFF"));
            }

            if (str != null && str.length() > 0) {
                viewLeft.setVisibility(View.VISIBLE);
            } else {
                viewLeft.setVisibility(View.INVISIBLE);
            }
        }
        holder.anScrView.setOnTouchListener((arg0, arg1) -> b);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class AdapterViewHolder extends RecyclerView.ViewHolder {

        LinearLayout ll_content;
        LinearLayout tab_root;
        TextView textView;
        LinearLayout ll_title;
        TextView textBody;
        LinearLayout ll_body;
        AnimateScrollView anScrView;


        public AdapterViewHolder(View itemView) {
            super(itemView);

            ll_content = (LinearLayout) itemView.findViewById(R.id.ll_content);
            ll_title = (LinearLayout) itemView.findViewById(R.id.ll_title);
            ll_body = (LinearLayout) itemView.findViewById(R.id.ll_body);
            textView = (TextView) itemView.findViewById(R.id.tv_title);
            textBody = (TextView) itemView.findViewById(R.id.tv);
            anScrView = (AnimateScrollView) itemView.findViewById(R.id.scrollView);
            tab_root = (LinearLayout) itemView.findViewById(R.id.tab_root);
            anScrView.setTag(recyclerView);



        }

    }

    private void addRow(LinearLayout ll_content, LinearLayout linearLayout, TextView[] textViews, View[] viewLefts, int size) {
        //添加row
//        LogUtils.i("addRow:linearLayout"+linearLayout.getChildCount());
        linearLayout.removeAllViews();
        int childCount = ll_content.getChildCount();
        // LogUtils.i("addRow:"+childCount);
        if (childCount != 3) {
            ll_content.removeViewAt(2);
        }
        for (int i = 0; i < size; i++) {
            View v = View.inflate(mContext, R.layout.pkshop_table, null);
            TextView con = (TextView) v.findViewById(R.id.wide_content);
            textViews[i] = con;
            View vlft = v.findViewById(R.id.view_left);
            viewLefts[i] = vlft;
            //默认是3个节点数
            if (isTop && i == 0) {
                ll_content.addView(v, 2);//位置节点2插入
            } else {
                //  int childCount = ll_content.getChildCount();
                // LogUtils.i("addRow:childCount"+childCount);
                linearLayout.addView(v);
            }

        }
    }


}
